<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE link PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<body >
<head>
	<link rel="stylesheet"  href="<c:url value='/resource/css/zTreeStyle/zTreeStyle.css'/>" type="text/css"></link>
	<script type="text/javascript" src="<c:url value='/resource/js/jquery-1.7.2.min.js'/>"></script>
	<script type="text/javascript" src="<c:url value='/resource/js/jquery.ztree.core-3.2.js'/>"></script>
	<script type="text/javascript" src="<c:url value='/resource/js/jquery.ztree.excheck-3.2.min.js'/>"></script>
	<script type="text/javascript" src="<c:url value='/resource/artdialog/artdialog.js?skin=${sys_theme}'/>"></script>
	<script type="text/javascript" src="<c:url value='/resource/js/common.js'/>"></script>
	<script type="text/javascript" src="<c:url value='/resource/artdialog/iframetools.js'/>"></script>
	<script type="text/javascript" src="<c:url value='/resource/js/map.js'/>"></script>
	<script type="text/javascript">
		var setting = {
			async : {
				enable : true,
				url : "/sacw/dept/subtree.json?dw1=<%=request.getParameter("dw1")%>",
				dataType : "json",
				type : 'get',
				autoParam : [ "deptId" ],
				dataFilter : function(treeId, parentNode, childNodes) {
					return childNodes.data;
				}
			},
			callback : {
				beforeExpand: beforeExpand,
				onExpand: onExpand
			},
			check:{
				enable:true,
				chkStyle:"radio",
				radioType:"all"
			},
			data : {
				key:{
					name:"alias"
				},
				simpleData : {
					enable : true,
					idKey : "deptId",
					pIdKey : "parentId",
					rootPId : 0
				}
			},
			edit:{
				enable:true,
				showRemoveBtn:false,
				showRenameBtn:false
			}
		};
		
		/**树初始化*/
		$(function() {
			$.fn.zTree.init($("#treeDemo"), setting);
		});
		
		/**同时只能展开一个节点，减少树的长度 start*/
		var curExpandNode = null;
		function beforeExpand(treeId, treeNode) {
			var pNode = curExpandNode ? curExpandNode.getParentNode():null;
			var treeNodeP = treeNode.parentTId ? treeNode.getParentNode():null;
			var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			for(var i=0, l=!treeNodeP ? 0:treeNodeP.children.length; i<l; i++ ) {
				if (treeNode !== treeNodeP.children[i]) {
					zTree.expandNode(treeNodeP.children[i], false);
				}
			}
			while (pNode) {
				if (pNode === treeNode) {
					break;
				}
				pNode = pNode.getParentNode();
			}
			if (!pNode) {
				singlePath(treeNode);
			}
	
		}
		function singlePath(newNode) {
			if (newNode === curExpandNode) return;
			if (curExpandNode && curExpandNode.open==true) {
				var zTree = $.fn.zTree.getZTreeObj("treeDemo");
				if (newNode.parentTId === curExpandNode.parentTId) {
					zTree.expandNode(curExpandNode, false);
				} else {
					var newParents = [];
					while (newNode) {
						newNode = newNode.getParentNode();
						if (newNode === curExpandNode) {
							newParents = null;
							break;
						} else if (newNode) {
							newParents.push(newNode);
						}
					}
					if (newParents!=null) {
						var oldNode = curExpandNode;
						var oldParents = [];
						while (oldNode) {
							oldNode = oldNode.getParentNode();
							if (oldNode) {
								oldParents.push(oldNode);
							}
						}
						if (newParents.length>0) {
							for (var i = Math.min(newParents.length, oldParents.length)-1; i>=0; i--) {
								if (newParents[i] !== oldParents[i]) {
									zTree.expandNode(oldParents[i], false);
									break;
								}
							}
						} else {
							zTree.expandNode(oldParents[oldParents.length-1], false);
						}
					}
				}
			}
			curExpandNode = newNode;
		}
	
		function onExpand(event, treeId, treeNode) {
			curExpandNode = treeNode;
		}
		/**end*/
		
		function sub_form(){
			var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			var nodes = zTree.getCheckedNodes(true);
			if(nodes&&nodes.length>0){
				var node = nodes[0];
				var origin = artDialog.open.origin;
				if("320500"==node.deptId){
					art.dialog.tips("不可以选择顶级部门",3000);
					return false;
				}
				try
				{
				if(node.alias){
					if(origin.document.getElementById('tmpDw2Name')){
						origin.document.getElementById('tmpDw2Name').value=node.alias;
					}
					if(origin.document.getElementById('tmpDw2Id')){
						origin.document.getElementById('tmpDw2Id').value=node.deptId;
					}
					origin.document.getElementById('dw2Id').value=node.deptId;
					origin.document.getElementById('dw2').value=node.alias;
					}
					if(typeof(origin.getbzqk)=='function'){
						origin.getbzqk();
					}
				}catch(e){
				  if(origin.document.getElementById('deptid')&&origin.document.getElementById('deptname')){
					origin.document.getElementById('deptid').value=node.deptId;
					origin.document.getElementById('deptname').value=node.name;
				  }
				}
				art.dialog.close();
			}
		}
		
</script>
</head>
<body style="margin:0px;width:400px;height:500px;">
	<div class="content_wrap" style="width:400px;height:500px;float:left;overflow-x:hidden;overflow-y:auto  ">
		<div class="zTreeDemoBackground left">
			<ul id="treeDemo" class="ztree" style="width:370px;"></ul>
		</div>
	</div>
</body>
</html>